<style lang="scss" scoped>
  .picImg {
    width: 40px;
    height: 40px;
  }

  .el-radio {
    margin: 10px 20px;
  }
</style>

<template>
  <div class="app-container">
    <!--&lt;!&ndash; 查询和其他操作 &ndash;&gt;-->
    <div class="filter-container">
      <el-form :inline="true" status-icon label-position="left" class="demo-form-inline" >
        <el-form-item label="卡BIN" prop="cardBin">
          <el-input v-model="listQuery.cardBin" placeholder="卡BIN"/>
        </el-form-item>
        <!-- <el-form-item label="卡名称" prop="cardName">
          <el-input v-model="listQuery.cardName" placeholder="卡名称"/>
        </el-form-item> -->
        <el-form-item label="银行代码" prop="bankCode">
          <el-input v-model="listQuery.bankCode" placeholder="银行代码"/>
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName">
          <el-input v-model="listQuery.bankName" placeholder="银行名称"/>
        </el-form-item>
        <el-form-item label="卡类型" prop="cardType">
          <el-select v-model="listQuery.cardType" placeholder="请选择">
            <el-option value="" label="全部">全部</el-option>
            <el-option value="1" label="借记卡">借记卡</el-option>
            <el-option value="2" label="信用卡选择">信用卡选择</el-option>
            <el-option value="3" label="准贷记卡">准贷记卡</el-option>
            <el-option value="4" label="预付卡">预付卡</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否是银联卡" prop="cardCategory">
          <el-select v-model="listQuery.cardCategory" placeholder="请选择">
            <el-option value="" label="全部">全部</el-option>
            <el-option value="0" label="否">否</el-option>
            <el-option value="1" label="是">是</el-option>
          </el-select>
          <!--<el-radio-group v-model="listQuery.cardCategory">-->
            <!--<el-radio label="0">否</el-radio>-->
            <!--<el-radio label="1">是</el-radio>-->
          <!--</el-radio-group>-->
        </el-form-item>
        <el-form-item>
          <el-button class="filter-item" type="primary" @click="queryList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-edit"
                 @click="handleAdd">新增
          </el-button>
          <el-button class="filter-item" type="primary" icon="el-icon-upload"
                 @click="handleExport">导入文件
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row
              :header-cell-style="{background:'#92D3FF',color:'#fff'}">

      <el-table-column align="center" min-width="100" label="卡BIN" prop="cardBin"/>
      <el-table-column align="center" min-width="100" label="卡长度" prop="binLen"/>
      <el-table-column align="center" min-width="100" label="银行代码" prop="bankCode"/>
      <el-table-column align="center" min-width="100" label="银行名称" prop="bankName"/>
      <el-table-column align="center" min-width="100" label="卡类型" prop="cardType">
        <template slot-scope="scope">
          <el-tag>{{{'1':'借记卡','2':'信用卡选择','3':'准贷记卡','4':'预付卡'}[scope.row.cardType]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="100" label="是否是银联卡" prop="cardCategory">
        <template slot-scope="scope">
          <el-tag>{{{'1':'是','0':'否'}[scope.row.cardCategory]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini"
                     @click="handleEdit(scope.row)">修改
          </el-button>
          <el-popover
            placement="top"
            width="200"
            trigger="manual"
            :ref="'popover-' + scope.row.cardBin">
            <p>确定删除该数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-` + scope.row.cardBin].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click="doDelete(scope.row)">确定</el-button>
            </div>
            <el-button type="danger" size="mini" slot="reference" @click="$refs[`popover-` + scope.row.cardBin].doShow()">删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
                @pagination="getList"/>
    <!-- 新增、编辑对话框 -->
    <el-dialog :visible.sync="editDialogVisible" :title="textMap[dialogStatus]" :close-on-click-modal="false">
      <el-form ref="editForm" :rules="editFormRules" :model="editForm" status-icon label-position="left"
               label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="卡BIN" prop="cardBin">
          <el-input :disabled="dialogStatus=='update'" v-model="editForm.cardBin" placeholder="卡BIN"/>
        </el-form-item>
        <el-form-item label="卡长度" prop="binLen">
          <el-input v-model="editForm.binLen" auto-complete="off" placeholder="卡长度"/>
        </el-form-item>
        <el-form-item label="银行代码" prop="bankCode">
          <el-select v-model="editForm.bankCode" @change="changeEditBank">
            <el-option v-for="item in bankCodeList" :key="item.bankCode" :label="item.bankCode"
                       :value="item.bankCode"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName">
          <el-input v-model="editForm.bankName" disabled  auto-complete="off" placeholder="银行名称"/>
        </el-form-item>
        <el-form-item label="卡类型" prop="cardType">
          <el-radio-group v-model="editForm.cardType">
            <el-radio label="1">借记卡</el-radio>
            <el-radio label="2">信用卡选择</el-radio>
            <el-radio label="3">准贷记卡</el-radio>
            <el-radio label="4">预付卡</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否是银联卡" prop="cardCategory">
          <el-radio-group v-model="editForm.cardCategory">
            <el-radio label="0">否</el-radio>
            <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <div slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button v-if="dialogStatus=='create'" type="primary" @click="doCreate">确定</el-button>
          <el-button v-else type="primary" @click="doEdit">确定</el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="exportDialogVisible" title="文件导入" :close-on-click-modal="false">
      <el-upload
        class="upload-demo"
        ref="upload"
        action="string"
        :before-upload="beforeUploadFile"
        :http-request="doUpload"
        :file-list="fileList"
        :limit="1"
        name="filename"
        :auto-upload="false">
        <el-button size="small" plain>选择文件</el-button>
        <div slot="tip" class="el-upload__tip">只能上传xlsx(Excel2007)文件，且不超过10M</div>
        <!--<el-button slot="trigger" size="small" type="primary">选取文件</el-button>-->
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button style="margin-left: 10px;" size="small" type="success" @click="exportDialogVisible=false">取消
        </el-button>
        <el-button style="margin-left: 10px;" size="small" type="success" @click="$refs.upload.submit()">确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { doGet, doPost, doFile } from '@/api'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  import checkPermission from '@/utils/permission' // 权限判断函数

  export default {
    name: 'bank',
    components: { Pagination },
    filters: {
      typeMapFilter(type) {
        return typeMap[type]
      }
    },
    data() {
      return {
        list: [],
        total: 0,
        listLoading: true,
        listQuery: {
          pageNum: 1,
          pageSize: 20,
          cardBin: undefined,
          cardName: undefined,
          bankCode: undefined,
          bankName: undefined,
          cardType: '',
          cardCategory: ''
        },
        bankCodeList: [],
        editForm: {
          cardBin: undefined,
          binLen: undefined,
          cardType: undefined,
          cardCategory: undefined,
          bankCode: undefined,
          bankName:undefined
        },
        editFormRules: {
          cardBin: [{ required: true, message: '卡BIN不允许为空', trigger: 'blur' }],
          binLen: [{ required: true, message: '卡名称不允许为空', trigger: 'blur' }],
          bankCode: [{ required: true, message: '银行名称不允许为空', trigger: 'blur' }]
        },
        editDialogVisible: false,
        dialogStatus: undefined,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        fileList: [],
        exportDialogVisible: false
      }
    },
    created() {
      this.getList()
      this.getBankList()
    },
    methods: {
      checkPermission,
      getBankList() {
        doGet('bank.html?op=list').then(response => {
          this.bankCodeList = response.rows
        }).catch(() => {
          this.bankCodeList=[]
        })
      },
      getList() {
        this.listLoading = true
        doGet('cardbin.html?op=list', this.listQuery).then(response => {
          this.list = response.rows
          this.total = response.data.totalcount
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      },
      handleAdd() {
        this.editForm = {
          cardBin: undefined,
          binLen: undefined,
          bankName: undefined,
          bankCode: undefined,
          cardType: "1",
          cardCategory: "1"
        }
        this.dialogStatus = 'create'
        this.editDialogVisible = true
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },
      changeEditBank(bankCode){
        console.log(bankCode)
        this.bankCodeList.map(item=>{
          if(item.bankCode==bankCode){
            this.editForm.bankName=item.bankName
          }
        })
      },
      doCreate() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doGet('cardbin.html?op=add', this.editForm)
              .then(response => {
                this.getList()
                this.editDialogVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '添加成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
      handleEdit(row) {
        this.editForm = Object.assign({}, row)
        this.editIndex = this.list.indexOf(row)
        this.dialogStatus = 'update'
        this.editDialogVisible = true
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },
      queryList(){
        this.listQuery.pageNum=1
        this.listQuery.pageSize=20
        this.getList()
      },
      doEdit() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doGet('cardbin.html?op=chg', this.editForm)
              .then(response => {
                this.editDialogVisible = false
                this.list[this.editIndex] = this.editForm
                this.$notify.success({
                  title: '成功',
                  message: '修改成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
      doDelete(row) {
        doGet('cardbin.html?op=del', row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
            this.$refs[`popover-` + row.cardBin].doClose()
            this.total--
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.message
            })
          })
      },
      /*==deleteEnd==*/
      /*==exportBegin==*/
      handleExport(value) {
        this.exportDialogVisible = true
      },
      beforeUploadFile(file) {
        console.log('before upload')
        console.log(file)
        let extension = file.name.substring(file.name.lastIndexOf('.') + 1)
        let size = file.size / 1024 / 1024
        if (extension !== 'xlsx' && extension !== 'xls') {
          this.$notify.warning({
            title: '警告',
            message: `只能上传Excel（即后缀是.xlsx或.xls）的文件`
          })
          return false
        }
      },
      doUpload(param) {
        let fd = new FormData()
        fd.append('filename', param.file)//传文件\
        doPost('cardbin.html?op=import', fd).then((res) => {
          this.getList()
          this.exportDialogVisible = false
          this.$notify.success({
            title: '成功',
            message: '上传成功'
          })
        }).catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.message
          })
        })
      }

    }
  }
</script>
